package com.bolt.report.business.modules.report.service;

import com.bolt.common.bean.BeanUtil;
import com.bolt.common.convert.Convert;
import com.bolt.common.lang.Assert;
import com.bolt.convention.exception.ServiceException;
import com.bolt.core.view.components.BindDataModel;
import com.bolt.core.view.components.FormItem;
import com.bolt.report.business.ReportDBDefManager;
import com.bolt.report.definition.ReportDefinition;
import com.bolt.report.engine.CollectionDataSource;
import com.bolt.report.engine.ReportConstants;
import com.bolt.report.exception.ReportException;
import com.bolt.support.ui.tree.model.TreeDataProvider;
import com.bolt.support.ui.tree.vo.UITreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(ReportConstants.FORM_SELECT_TREE_BIND_DATA_NAME)
@Slf4j
public class FormSelectTreeBindData implements TreeDataProvider {

    @Autowired
    private ReportDBDefManager ReportDBDefManager;


    @Autowired
    private FormItemBindDataService formItemBindDataService;

    @Override
    public List<UITreeNode> getChildren(Object parentId, Map params) {
        if (!params.containsKey(ReportConstants.REPORT_ID_PARAM)) {
            throw new ReportException("表报参数未指定");
        }
        if (!params.containsKey(ReportConstants.REPORT_FORM_ITEM_ID)) {
            throw new ReportException("控制参数未指定");
        }
        String reportId = (String) params.get(ReportConstants.REPORT_ID_PARAM);
        String itemId = (String) params.get(ReportConstants.REPORT_FORM_ITEM_ID);
        ReportDefinition definition = ReportDBDefManager.loadReportDefinition(reportId);
        FormItem formItem = definition.findFormItem(itemId);
        Assert.isNull(formItem, "未找到控件");
        Assert.isNull(formItem.getBindDataModel(), "控件未配置数据绑定");
        params.putAll(formItem.getParameters());

        List<UITreeNode> children = new ArrayList<>();
        try {
            CollectionDataSource dataSource;
            if (parentId == null || parentId.equals(0L)) {
                dataSource = (CollectionDataSource) formItemBindDataService.executeDataSet(formItem.getBindDataModel(), params);
            } else {
                params.put(ReportConstants.TREE_DATA_PARAM, parentId);
                dataSource = (CollectionDataSource) formItemBindDataService.executeDataSet(formItem.getBindDataModel(), params);

            }
            while (dataSource.next()){
                UITreeNode treeNode = new UITreeNode();
                converterTreeNode(dataSource,treeNode,formItem.getBindDataModel());
                children.add(treeNode);
            }

        } catch (Exception ex) {
            log.error(ex.toString());
            throw new ServiceException(ex);

        }
        return children;

    }

    protected void converterTreeNode(CollectionDataSource dataSource, UITreeNode treeNode, BindDataModel bindData) {
        treeNode.setId(dataSource.getFieldValue(bindData.getKeyField()));
        treeNode.setTitle(dataSource.getFieldValue(bindData.getLabelField()));
        treeNode.setParent(Convert.toBool(dataSource.getFieldValue(bindData.getTreeParentField()),true));
        Object data = dataSource.getCurrentData();
        Map attribute ;
        if(data instanceof  Map){
            attribute = (Map)data;
        }else{
            attribute = BeanUtil.beanToMap(data);
        }
        attribute.remove("id");
        attribute.remove("title");
        treeNode.putAllAttribute(attribute);
    }

}